package com.edu.dk.security.util;

public class StringUtils {
	public static final String EMPTY = "";

	public static String replace(String text, String repl, String with) {
		return replace(text, repl, with, -1);
	}

	public static String replace(String text, String repl, String with, int max) {
		if (text == null || repl == null || with == null || repl.length() == 0 || max == 0) {
			return text;
		}

		StringBuilder buf = new StringBuilder(text.length());
		int start = 0;
		int end = 0;

		while ((end = text.indexOf(repl, start)) != -1) {
			buf.append(text.substring(start, end)).append(with);
			start = end + repl.length();

			if (--max == 0) {
				break;
			}
		}

		buf.append(text.substring(start));

		return buf.toString();
	}

	public static String trimToNull(String str) {
		if (str == null) {
			return null;
		}

		String result = str.trim();

		if (result == null || result.length() == 0) {
			return null;
		}

		return result;
	}

	public static boolean isEmpty(String str) {
		return str == null || str.length() == 0;
	}

	public static String trimToEmpty(String str) {
		if (str == null) {
			return EMPTY;
		}

		return str.trim();
	}

	public static boolean isBlank(String str) {
		int length;

		if (str == null || (length = str.length()) == 0) {
			return true;
		}

		for (int i = 0; i < length; i++) {
			if (!Character.isWhitespace(str.charAt(i))) {
				return false;
			}
		}

		return true;
	}

	/**
	 * 检查字符串中是否包含指定的字符。如果字符串为<code>null</code>，将返回<code>false</code>。
	 * <p/>
	 * 
	 * <pre>
	 * StringUtil.contains(null, *)    = false
	 * StringUtil.contains(BasicConstant.EMPTY_STRING, *)      = false
	 * StringUtil.contains("abc", 'a') = true
	 * StringUtil.contains("abc", 'z') = false
	 * </pre>
	 *
	 * @param str
	 *            要扫描的字符串
	 * @param searchChar
	 *            要查找的字符
	 * @return 如果找到，则返回<code>true</code>
	 */
	public static boolean contains(String str, char searchChar) {
		if (str == null || str.length() == 0) {
			return false;
		}

		return str.indexOf(searchChar) >= 0;
	}

	/**
	 * 检查字符串中是否包含指定的字符串。如果字符串为<code>null</code>，将返回<code>false</code>。
	 * <p/>
	 * 
	 * <pre>
	 * StringUtil.contains(null, *)     = false
	 * StringUtil.contains(*, null)     = false
	 * StringUtil.contains(BasicConstant.EMPTY_STRING, BasicConstant.EMPTY_STRING)      = true
	 * StringUtil.contains("abc", BasicConstant.EMPTY_STRING)   = true
	 * StringUtil.contains("abc", "a")  = true
	 * StringUtil.contains("abc", "z")  = false
	 * </pre>
	 *
	 * @param str
	 *            要扫描的字符串
	 * @param searchStr
	 *            要查找的字符串
	 * @return 如果找到，则返回<code>true</code>
	 */
	public static boolean contains(String str, String searchStr) {
		if (str == null || searchStr == null) {
			return false;
		}

		return str.indexOf(searchStr) >= 0;
	}

	public static String substringBefore(String str, String separator) {
		if (str == null || separator == null || str.length() == 0) {
			return str;
		}

		if (separator.length() == 0) {
			return EMPTY;
		}

		int pos = str.indexOf(separator);

		if (pos == -1) {
			return str;
		}

		return str.substring(0, pos);
	}

	public static String substringAfter(String str, String separator) {
		if (str == null || str.length() == 0) {
			return str;
		}

		if (separator == null) {
			return EMPTY;
		}

		int pos = str.indexOf(separator);

		if (pos == -1) {
			return EMPTY;
		}

		return str.substring(pos + separator.length());
	}
}
